home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / cvttobm.pro < prev    next >
Text File  |  1997-07-08  |  3KB  |  122 lines

  1. ; $Id: cvttobm.pro,v 1.5 1997/01/15 03:11:50 ali Exp $
  2. ;
  3. ; Copyright (c) 1996-1997, Research Systems, Inc.  All rights reserved.
  4. ;    Unauthorized reproduction prohibited.
  5. ;
  6. ;+
  7. ; NAME: Cvttobm
  8. ;
  9. ; PURPOSE:
  10. ;    Converts a byte array in which each byte represents one pixel
  11. ;       into a bitmap byte array in which each bit represents one
  12. ;       pixel. This is useful when creating bitmap labels for buttons
  13. ;       created with the WIDGET_BUTTON function.
  14. ;
  15. ;       Bitmap byte arrays are monochrome; by default, CVTTOBM converts
  16. ;       pixels that are darker than the median value to black and pixels
  17. ;       that are lighter than the median value to white. You can supply
  18. ;       a different threshold value via the THRESHOLD keyword.
  19. ;
  20. ;       Most of IDL's image file format reading functions (READ_BMP,
  21. ;       READ_PICT, etc.) return a byte array which must be converted
  22. ;       before use as a button label. Note that there is one exception
  23. ;       to this rule; the READ_X11_BITMAP routine returns a bitmap
  24. ;       byte array that needs no conversion before use.
  25. ;
  26. ; CATEGORY:
  27. ;
  28. ;       Widgets, button bitmaps
  29. ;
  30. ; CALLING SEQUENCE:
  31. ;
  32. ;    bitmap = Cvttobm(array [,THRESHOLD = Threshold])
  33. ; INPUTS:
  34. ;    array - A 2-dimensional pixel array, one byte per pixel
  35. ;
  36. ;
  37. ; OPTIONAL INPUTS:
  38. ;       None
  39. ;
  40. ;
  41. ; KEYWORD PARAMETERS:
  42. ;
  43. ;    THRESHOLD - A byte value (or an integer value between 0 and 255)
  44. ;                   to be used as a threshold value when determining if
  45. ;                   a particular pixel is black or white. If not specified,
  46. ;                   the threshold is calculated to be the average of the
  47. ;                   input array.
  48. ;
  49. ; OUTPUTS:
  50. ;    bitmap - bitmap byte array, in which each bit represents one pixel
  51. ;
  52. ;
  53. ; OPTIONAL OUTPUTS:
  54. ;       None
  55. ;
  56. ;
  57. ; COMMON BLOCKS:
  58. ;       None
  59. ;
  60. ;
  61. ; SIDE EFFECTS:
  62. ;       None
  63. ;
  64. ;
  65. ; RESTRICTIONS:
  66. ;       None
  67. ;
  68. ;
  69. ; PROCEDURE:
  70. ; 1. Creates mask from input array, where values are 0/1 based on threshold.
  71. ; 2. Calculates the size of the output array.
  72. ; 3. Calculates the bitmap array from byte array based on mask.
  73. ;
  74. ; EXAMPLE:
  75. ;
  76. ; IDL> image=bytscl(dist(100))
  77. ; IDL> base=widget_base(/column)
  78. ; IDL> button=widget_button(base,value=Cvttobm(image))
  79. ; IDL> widget_control,base,/realize
  80. ;
  81. ;
  82. ; MODIFICATION HISTORY:
  83. ;       Created: Mark Rehder, 10/96
  84. ;       Modified: Lubos Pochman, 10/96
  85. ;-
  86.  
  87. function Cvttobm, array, THRESHOLD=threshold
  88.  
  89.     s = size(array)
  90.     if s[0] ne 2 then message, "Input array is not a 2D array!"
  91.     ;
  92.     ; Check the THRESHOLD keyword
  93.     ;
  94.     mask=bytscl(reverse(array,2))
  95.     if (N_ELEMENTS(THRESHOLD) eq 0) then threshold=total(mask)/n_elements(mask)
  96.     ;
  97.     ; Calculate mask based on threshold
  98.     ;
  99.     mask[where(mask lt threshold)]=0b
  100.     mask[where(mask ge threshold)]=1b
  101.     ;
  102.     ; Calculate the new size of the bitmap array
  103.     ;
  104.     s=size(mask) & cols=((s[1]-1)/8+1) & rows=s[2]
  105.     bmp=bytarr(cols,rows)
  106.     mult=[[1],[2],[4],[8],[16],[32],[64],[128]]
  107.     ;
  108.     ; Calculate the bitmap array from byte array based on mask
  109.     ;
  110.     vect=intarr(8)
  111.     for i=0,cols-1 do begin
  112.         for j=0,rows-1 do begin
  113.             temp=reform(mask[i*8:min([s[1]-1,i*8+7]),j])
  114.             vect[0:N_ELEMENTS(temp)-1]=temp
  115.             bmp[i,j]=byte(vect##mult)
  116.         endfor
  117.     endfor
  118.  
  119.     return,bmp
  120. end
  121.